package com.system.service;

import java.util.ArrayList;
import java.util.List;

import com.system.dao.CourseDao;
import com.system.dao.DaoFactory;
import com.system.dao.TeacherDao;
import com.system.json.JsonContext;
import com.system.model.Course;
import com.system.model.CoursePojo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CourseService {
	private DaoFactory factory = DaoFactory.getInstance();
	private JsonContext context = new JsonContext();
	
	//查询所有课程信息
	public JSONObject queryAll() {
		CourseDao courseDao = factory.createCourseDao();
		ArrayList<Course> courses = courseDao.findAll();
		if(courses==null){
			return context.getFailedObject("课程列表为空");
		}else{
			JSONArray array = new JSONArray();
			for(int i = 0; i < courses.size(); i++) {
				Course course = courses.get(i);
				JSONObject json=JSONObject.fromObject(course);
				array.add(json);
			}
			return context.getSuccessObject("查询成功", null, array);
		}
	}
	//根据courseName来查询课程信息
	public JSONObject queryInfo(String courseName) {
		CourseDao courseDao = factory.createCourseDao();
		Course course = courseDao.findByName(courseName);
		if(course==null){
			return context.getFailedObject("该课程不存在");
		}else{
			JSONObject json=JSONObject.fromObject(course);
			return context.getSuccessObject("查询成功", json, null);
		}
	}
	//添加课程信息
	public JSONObject add(Course course) {
		CourseDao courseDao = factory.createCourseDao();
		boolean res = courseDao.add(course);
		if(!res){
			return context.getFailedObject("添加课程信息失败");
		}else{
			return context.getSuccessObject("添加课程信息成功", null, null);
		}
	}
	//修改学生信息
	public JSONObject modify(Course course) {
		CourseDao courseDao = factory.createCourseDao();
		boolean res = courseDao.modify(course);
		if(!res){
			return context.getFailedObject("修改课程信息失败");
		}else{
			return context.getSuccessObject("修改课程信息成功", null, null);
		}
	}
	//删除课程信息
	public JSONObject deleteCourses(String[] names) {
		CourseDao courseDao = factory.createCourseDao();
		boolean res = courseDao.deleteCourses(names);
		if(!res){
			return context.getFailedObject("删除课程信息失败");
		}else{
			return context.getSuccessObject("删除课程信息成功", null, null);
		}
	}
	
	public JSONObject checkoutListCourses(){
		CourseDao courseDao=factory.createCourseDao();
		List<CoursePojo> list=courseDao.findAllCourse();
		if(list.isEmpty())
			return context.getFailedObject("查询为空");
		else{
			JSONArray array=new JSONArray();
			for(int i=0;i<list.size();++i){
				CoursePojo course=list.get(i);
				JSONObject json=JSONObject.fromObject(course);
				array.add(json);
			}
			return context.getSuccessObject("查询成功", null, array);
		}
		
	}
	/**
	 * 根据职工号查询相关课程信息
	 * @param teanum
	 * @return
	 */
	public JSONObject checkoutqueryCoursebyTeanum(String teanum){
		TeacherDao tdao=factory.createTeacherDao();
		CourseDao cdao=factory.createCourseDao();
		Integer tea_id=tdao.findId(teanum);
		List<String> list=cdao.listCourse(tea_id);
		if(list==null)
			return context.getFailedObject("查询失败");
		else if(list.isEmpty())
			return context.getFailedObject("查询结果为空");
		else{
			JSONArray arr=JSONArray.fromObject(list);
			return context.getSuccessObject("查询成功", null, arr);
		}
	}
}
